package com.qiyuesuo.server.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qiyuesuo.server.PO.FileInfo;
import com.qiyuesuo.server.dao.FileInfoDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 获取最新上传文件的信息
 * 返回ArrayList<FileInfo> json
 *
 * @author s77
 * @create 2020-4月-03 15:11
 */
public class NewFileInfoServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int n = Integer.parseInt(req.getParameter("num"));
        FileInfoDao dao = new FileInfoDao();
        List<FileInfo> infos = dao.selectNew(n);
        ObjectMapper mapper = new ObjectMapper();
        String fileInfoJson = mapper.writeValueAsString(infos);
        //写出到响应
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(fileInfoJson);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
